package com.tjcd.project.library_base.common.widget;

import android.app.Activity;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.tjcd.project.library_base.R;

/**
 * @Author junweiliu
 * @Description 异常页工具类
 * @Version 1.0
 * @CreateDate 2019/12/27
 * @QQ 1007271386
 */
public class ThrowUtil {
    /**
     * 默认距离顶部距离
     */
    private final int DEFAULT_MARGIN_TOP = 85;
    /**
     * 异常布局
     */
    ThrowLayout mThrowL;
    /**
     * 当前的类型
     */
    int mStatus;

    public void init(Object context) {
        try {
            if (null != context) {
                if (context instanceof Activity) {
                    mThrowL = ((Activity) context).findViewById(R.id.common_throw_layout);
                } else if (context instanceof View) {
                    mThrowL = ((View) context).findViewById(R.id.common_throw_layout);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e("如需使用公共异常布局,请先在布局文件中添加异常布局");
        }
    }

    /**
     * 隐藏异常页
     */
    public void hideThrowLayout() {
        mStatus = 0;
        if (null != mThrowL) {
            mThrowL.hide();
        }
    }

    /**
     * 异常页展示
     *
     * @param message
     * @param res
     * @param btnMsg
     * @param status
     * @param listener
     */
    public void showThrowLayout(String message, String msg, int res, String btnMsg, @ThrowLayout.EmptyStatus int status, int marginT, ThrowLayout.OnRetryListener listener) {
        if (null != mThrowL) {
            mStatus = status;
            mThrowL.setCustomMessage(message, msg, res, btnMsg, marginT);
            mThrowL.setRetryListener(listener);
            mThrowL.setEmptyStatus(status);
        }
    }

    /**
     * 空数据
     *
     * @param message
     */
    public void showNoDataLayout(String message) {
        showNoDataLayout(message, "", null);
    }

    /**
     * 空数据
     *
     * @param message
     */
    public void showNoDataLayout(String message, int res) {
        showNoDataLayout(message, "", res, "", null);
    }

    /**
     * 空数据
     *
     * @param message
     * @param btnMsg
     * @param listener
     */
    public void showNoDataLayout(String message, String btnMsg, ThrowLayout.OnRetryListener listener) {
        showNoDataLayout(message, "", R.mipmap.ic_launcher, btnMsg, listener);
    }

    /**
     * 空数据
     *
     * @param message
     * @param msg
     */
    public void showNoDataLayout(String message, String msg) {
        showNoDataLayout(message, msg, R.mipmap.ic_launcher, "", null);
    }

    /**
     * 空数据
     *
     * @param message
     * @param res
     * @param btnMsg
     * @param listener
     */
    public void showNoDataLayout(String message, String msg, int res, String btnMsg, ThrowLayout.OnRetryListener listener) {
        showThrowLayout(message, msg, res, btnMsg, ThrowLayout.STATUS_NO_DATA, DEFAULT_MARGIN_TOP, listener);
    }

    /**
     * 异常页
     *
     * @param message
     */
    public void showErrorsLayout(String message) {
        showErrorsLayout(message, "", null);
    }

    /**
     * 异常页
     *
     * @param message
     */
    public void showErrorsLayout(String message, int res) {
        showErrorsLayout(message, "", res, "", null);
    }

    /**
     * 异常页
     *
     * @param message
     */
    public void showErrorsLayout(String message, String msg) {
        showErrorsLayout(message, msg, R.mipmap.ic_launcher, "", null);
    }

    /**
     * 异常页
     *
     * @param message
     * @param btnMsg
     * @param listener
     */
    public void showErrorsLayout(String message, String btnMsg, ThrowLayout.OnRetryListener listener) {
        showErrorsLayout(message, "", R.mipmap.ic_launcher, btnMsg, listener);
    }

    /**
     * 异常页
     *
     * @param message
     * @param res
     * @param btnMsg
     * @param listener
     */
    public void showErrorsLayout(String message, String msg, int res, String btnMsg, ThrowLayout.OnRetryListener listener) {
        showThrowLayout(message, msg, res, btnMsg, ThrowLayout.STATUS_ERROR, DEFAULT_MARGIN_TOP, listener);
    }

    /**
     * 自定义
     *
     * @param message
     */
    public void showCustomLayout(String message) {
        showCustomLayout(message, "", null);
    }

    /**
     * 自定义
     *
     * @param message
     */
    public void showCustomLayout(String message, int res) {
        showCustomLayout(message, "", res, "", DEFAULT_MARGIN_TOP, null);
    }

    /**
     * 自定义
     *
     * @param message
     */
    public void showCustomLayout(String message, String msg, int res) {
        showCustomLayout(message, msg, res, "", DEFAULT_MARGIN_TOP, null);
    }

    /**
     * 自定义
     *
     * @param message
     * @param btnMsg
     * @param listener
     */
    public void showCustomLayout(String message, String btnMsg, ThrowLayout.OnRetryListener listener) {
        showCustomLayout(message, "", R.mipmap.ic_launcher, btnMsg, DEFAULT_MARGIN_TOP, listener);
    }

    /**
     * 自定义
     *
     * @param message
     * @param res
     * @param btnMsg
     * @param mariginT
     * @param listener
     */
    public void showCustomLayout(String message, String msg, int res, String btnMsg, int mariginT, ThrowLayout.OnRetryListener listener) {
        showThrowLayout(message, msg, res, btnMsg, ThrowLayout.STATUS_CUSTOME, mariginT, listener);
    }

    /**
     * 设置图片显示隐藏
     *
     * @param visable
     */
    public void changeImageVisable(int visable) {
        if (null != mThrowL) {
            mThrowL.setImageVisable(visable);
        }
    }

    /**
     * 设置marginTop
     *
     * @param top
     */
    public void setMarginTop(int top) {
        if (null != mThrowL) {
            mThrowL.setMarginTop(top);
        }
    }

    /**
     * 获取当前的状态
     *
     * @return
     */
    public int getStatus() {
        return mStatus;
    }

}
